package com.hhsjdesign.dynamicui.utils

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.hhsjdesign.dynamicui.core.property.ViewBaseProperty
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.property.viewdata.ItemViewData
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetTypeConst
import java.lang.reflect.Constructor

/**
 * 将视图数据转成视图树
 * View 和 LayoutParams
 */
object ViewTreeTransformer {

    //创建View，将ItemViewWidget转成 View
    fun toViewTree(context: Context, itemWidgetModel: ItemViewData): View {
        val cons: Constructor<*> =
            ItemViewData.viewClass(itemWidgetModel.type).getConstructor(Context::class.java)
        val view: View = cons.newInstance(context) as View
        //控件的作用，设置控件业务
        itemWidgetModel.viewTag?.let {
            view.tag = "segi_${it}"
        }
        return view
    }

    //通过参数，创建layoutParam
    /**
     * @param parentLayoutType 父组件类型
     * @param property 视图属性
     */
    fun marginLayout(
        parentLayoutType: String?,
        property: ViewBaseProperty
    ): ViewGroup.MarginLayoutParams {

        val layoutParams: ViewGroup.MarginLayoutParams =
            //父布局是帧布局
            if (parentLayoutType == ItemViewData.ITEM_VIEW_TYPE_FRAME) {
                FrameLayout.LayoutParams(
                    property.size[1],
                    property.size[3]
                ).apply {
                    gravity = property.viewGravity()
                }
            }
            //线性布局
            else if (parentLayoutType == ItemViewData.ITEM_VIEW_TYPE_LINEAR_H
                || parentLayoutType == ItemViewData.ITEM_VIEW_TYPE_LINEAR_V
            ) {
                LinearLayout.LayoutParams(
                    property.size[1],
                    property.size[3]
                ).apply {
                    gravity = property.viewGravity()
                    //weight
                    if (property.size[0] == 1 && parentLayoutType == ItemViewData.ITEM_VIEW_TYPE_LINEAR_H) {
                        this.weight = property.size[1].toFloat()
                        this.width = 0
                    } else if (property.size[0] == 1 && parentLayoutType == ItemViewData.ITEM_VIEW_TYPE_LINEAR_V) {
                        this.weight = property.size[3].toFloat()
                        this.height = 0
                    }
                }
            }
            //其他类型，也就是组件布局
            else {
                //grid-h
                if (parentLayoutType == WidgetTypeConst.GRID_HORIZONTAL) {
                    ViewGroup.MarginLayoutParams(
                        (if (property.size[3] > 0)
                            property.size[3]
                        else
                            ViewGroup.LayoutParams.MATCH_PARENT),
                        (if (property.size[3] > 0)
                            property.size[3]
                        else
                            ViewGroup.LayoutParams.MATCH_PARENT)
                    )
                }
                //grid-v
                else if (parentLayoutType == WidgetTypeConst.GRID_VERTICAL) {
                    ViewGroup.MarginLayoutParams(
                        (if (property.size[3] > 0)
                            property.size[3]
                        else
                            ViewGroup.LayoutParams.MATCH_PARENT),
                        (if (property.size[3] > 0)
                            property.size[3]
                        else
                            ViewGroup.LayoutParams.MATCH_PARENT)
                    )
                }
                //banner 整个
                else if (parentLayoutType == WidgetTypeConst.BANNER_SINGLE) {
                    ViewGroup.MarginLayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                }
                //banner grid
                else if (parentLayoutType == WidgetTypeConst.BANNER_GRID) {
                    ViewGroup.MarginLayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                } else {
                    ViewGroup.MarginLayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        if (property.size[3] > 0)
                            property.size[3]
                        else
                            ViewGroup.LayoutParams.MATCH_PARENT
                    )
                }
            }
        return layoutParams
    }

}